package com.lottery.shadowbd.ui.fragment;


import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.callback.ECallback;
import com.lottery.shadowbd.io.api.LotApiIO;
import com.lottery.shadowbd.tuple.Tuple2;
import com.lottery.shadowbd.widget.DDTrendChart;
import com.lottery.shadowbd.widget.LottoTrendView;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.TreeSet;

/**
 * A simple {@link Fragment} subclass.
 */
public class LotSSQTrendFragment extends BaseFragment implements DDTrendChart.ISelectedChangeListener {

    private final int OK = 0x01;
    private final int OTHER = 0x02;

    private View mView;

    private LottoTrendView ltv_trendView;

    private DDTrendChart mTrendChart;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            dismissDialog();
            switch (msg.what) {
                case OK:
                    mTrendChart.updateData("01", (ArrayList) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected View initView(LayoutInflater inflater) {
        mView = inflater.inflate(R.layout.fragment_lot_ssqtrend, null);

        ltv_trendView = (LottoTrendView) mView.findViewById(R.id.ltv_trendView);

        return mView;
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        mTrendChart = new DDTrendChart(mContext, ltv_trendView);
        ltv_trendView.setChart(mTrendChart);
        mTrendChart.setShowYilou(true);
        mTrendChart.setDrawLine(true);
        mTrendChart.setSelectedChangeListener(this);

        getLotTrend();
    }

    @Override
    protected void listener() {

    }

    @Override
    protected void widgetClick(View v) {

    }

    private void getLotTrend() {
        showLoadingDialog();

        LotApiIO.getInstance().getLotTrendList(new ECallback<ArrayList, Tuple2<Integer, InputStream>>() {
            @Override
            public void onSuccess(ArrayList res) {
                mHandler.sendMessage(mHandler.obtainMessage(OK, res));
            }

            @Override
            public void onError(Tuple2<Integer, InputStream> error) {
                mHandler.sendEmptyMessage(OTHER);
            }

            @Override
            public void onThrowable(Throwable e) {
                mHandler.sendEmptyMessage(OTHER);
            }
        });
    }

    @Override
    public void onSelectedChange(TreeSet<Integer> treeSet, TreeSet<Integer> treeSet2) {

    }
}
